RESTOREFILE=$1
RESTOREDIR=`echo $1 | sed "s/.tar.gz//"`
usage()
{
	echo "Usage: $0 <restore_file>"
}

if test -z $RESTOREFILE; then
	usage
	exit 1
fi

tar xzvf $RESTOREFILE
if test "$?" != 0; then
	echo "Error unpacking the backup."
	exit 1
fi

if ! test -f $RESTOREDIR/wrt-nvram.bin; then
	echo "Restore directory does not contain wrt-nvram.bin"
	exit 1
fi
if ! test -f $RESTOREDIR/wrt-linux.trx; then
	echo "Restore directory does not contain wrt-linux.trx"
	exit 1
fi

echo
echo "Files ok, restoring started..."
echo

dd if=$RESTOREDIR/wrt-nvram.bin of=/dev/mtdblock/3
mount -o remount,ro /dev/mtdblock/4 /
mtd -r write $RESTOREDIR/wrt-linux.trx linux

exit 0
